package org.smog.core.dto.input;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.smog.core.dto.base.BaseDto;

/**
 * 分页查询基类
 *
 * @Description: Created by IntelliJ IDEA.
 * @project_name: smogTemp
 * @time: 2020-05-16 11:10
 * @email: 17685306043@163.com
 * @author: huangZhongYao
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
public class BaseQueryPageInput extends BaseDto {
    private static final long serialVersionUID = -2180816832795987101L;

    /**
     * 最小页码
     */
    private static final Long MIN_CURRENT = 1L;

    /**
     * 最小条数
     */
    private static final Long MIN_SIZE = 10L;

    @ApiModelProperty(
            value = "当前页码 默认第一页",
            required = true, example = "1"
    )
    Long current = MIN_CURRENT;

    @ApiModelProperty(
            value = "每页显示条数 默认10条",
            required = true, example = "10"
    )
    Long size = MIN_SIZE;

    public Long getCurrent() {
        return current;
    }

    public BaseQueryPageInput(long current, long size) {
        if (current > MIN_CURRENT) {
            this.current = current;
        }

        if (size > MIN_SIZE) {
            this.size = size;
        }

    }

    /**
     * 转mybatis 分页插件参数
     *
     * @return
     */
    public Page toMybatisPageObject() {
        return new Page(this.current, this.size);
    }

    /**
     * 手动分页,断言分页参数
     */
    public void assertion() {
        if (this.getCurrent() <= 1) {
            this.setCurrent(0L);
        } else {
            this.setCurrent((this.getCurrent() - 1) * this.getSize());
        }
    }
}
